package com.tjetc.view.studentview;

import com.github.pagehelper.PageInfo;
import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.CourseComment;
import com.zhiyun.domain.Student;
import com.zhiyun.service.CourseCommentService;
import com.zhiyun.service.StudentService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;

public class MyCourseView_In_Comment {
    StudentService studentService=CilentProxy.getProxyInstance(StudentService.class);
    CourseCommentService courseCommentService= CilentProxy.getProxyInstance(CourseCommentService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course){
        while (true){
            System.out.println("1.所有评价");
            System.out.println("2.评价");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allComment(course);
                    break;
                case 2:
                    insComment(course);
                    break;
                case 0:
                    return;
            }
        }
    }

    private void allComment(Course course) {
        int a=1;
        boolean boo1=true;
        while (true){
            boolean boo=true;
            Result courseCommentByIdAndPage = courseCommentService.findCourseCommentByIdAndPage(course.getId(), a);
            if (courseCommentByIdAndPage.getCode()==200){
                PageInfo<CourseComment> obj = (PageInfo<CourseComment>) courseCommentByIdAndPage.getObj();
                while (boo){
                    List<CourseComment> list = obj.getList();
                    for (CourseComment courseComment : list) {
                        Result result = studentService.myInfo(courseComment.getStudentId());
                        if (result.getCode()==200){
                            Student obj1 = (Student) result.getObj();
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


                            if (obj1!=null){
                                // 将Date转换为String

                                System.out.println("id:"+courseComment.getId()+"     评价:"+courseComment.getComment()+"  评价学生:"+obj1.getTruename());
                            }
                        }

                    }
                    System.out.println(obj.getPageNum()+"/"+obj.getPages());
                    System.out.println("请输入 上一页:0  下一页:1 退出:-1 ");
                    int i = scanner.nextInt();
                    switch (i){
                        case 0:
                            int prePage = obj.getPrePage();
                            a=prePage;
                            boo=false;
                            break;
                        case 1:
                            int nextPage = obj.getNextPage();
                            a=nextPage;
                            boo=false;
                            break;
                        case -1:
                            return;
                    }
                }
            }

        }
    }

    private void insComment(Course course) {
        while (true){
            System.out.println("请输入你的评价:");
            String pingjia = scanner.next();
            Result result = courseCommentService.addCourseComment(new CourseComment(0, pingjia, AppData.student.getId(), course.getId()));
            if (result.getCode()==200){
                System.out.println("评论成功");
            }else {
                System.out.println("评论失败");
            }
            break;
        }
    }
}
